import recom from '@/components/regionComponent.vue'
export default {
  components: {
    recom
  },
  data() {
    return {
      //添加完成的车
      addComplete: {
        image: 3841, //车图片--系列id
        carbrand: '', //品牌
        carseries: '', //系列
        toconfigure: '', //配置
      },
      specifAll: {
        brandlist: [], //品牌
        Serieslist: [], //系列
        Configulist: [], //车子
      },
      isAgree: true, //false我同意选择  true未选择
      showPopup: false, //弹框显隐
      second: 60, //验证码60S
      showText: true, //重新发送验证码
      registernot: false, //手机号是否注册 true 未登录， false登录了
      regtype: false, //true 未登录， false登录了
      grname: '', //姓名
      mobile: '', //手机号
      verificationCode: '', //验证码
      showdz: false, //车辆所在地
      province: '', //省份
      city: '', //城市
      derfimg: true, //是否显示默认图
      list: {}
    }
  },
  onLoad(e) {
    if (e.list === undefined) {

    } else {
      this.list = JSON.parse(e.list)
      if (this.list.type === 110) {
        console.log('e.list', this.list)
        this.addComplete.carbrand = this.list.brand //品牌    bid
        this.addComplete.image = this.list.car_Type_Id //系列id
        this.addComplete.carseries = this.list.car_Type_Name //系列   carTypeId
        this.addComplete.toconfigure = this.list.configuration_Name //配置   id
        this.specifAll.brandlist.bid = this.list.bid //品牌Id
        this.specifAll.brandlist.brand = this.list.brand //品牌名称
        this.specifAll.Serieslist.carTypeId = this.list.car_Type_Id //系列Id
        this.specifAll.Serieslist.carTypeName = this.list.car_Type_Name //系列名称
        this.specifAll.Configulist.id = Number(this.list.carid) //配置Id
        this.specifAll.Configulist.name = this.list.configuration_Name //配置名称
      }
    }

    //缓存中取到姓名和手机号
    const value = uni.getStorageSync('Ztc_C_USER_INFO')
    if (value) {
      this.grname = value.trueName
      this.mobile = value.mobile
      this.regtype = true
    } else {
      //如果没有登录，直接显示输入验证码
      this.registernot = true
      this.regtype = false
    }
    // 选择品牌，系列，车子
    uni.$on('carpction', data => {
      Object.assign(this.specifAll, data)
      this.addComplete.image = 3841
      this.addComplete.carbrand = this.specifAll.brandlist.brand //品牌    bid
      if (this.specifAll.Serieslist.carTypeId !== undefined) {
        this.addComplete.image = this.specifAll.Serieslist.carTypeId //系列id
      }
      this.addComplete.carseries = this.specifAll.Serieslist.carTypeName //系列   carTypeId
      this.addComplete.toconfigure = this.specifAll.Configulist.name //配置   id
    })
  },
  onUnload() {
    uni.$off('carpction')
  },
  methods: {
    //返回
    returnpage() {
      uni.navigateBack()
    },
    //图片报错
    imageError: function(e) {
      // console.error('image发生error事件，携带值为1' + e.detail.errMsg)
      // console.error('image发生error事件，携带值为2' + e.type)
      if (e.type === 'error') {
        this.derfimg = false
      }
    },
    //车辆所在地
    confirmdz(e) {
      this.province = e.province.label
      this.city = e.city.label
      this.showdz = false
    },
    close() {
      this.showdz = false
    },
    //倒计时
    getCode() {
      this.showText = false
      let times = 60
      this.interval = setInterval(() => {
        if (times > 1) {
          times--
          this.second = times < 10 ? '0' + times : times //小于10秒补 0
        } else {
          clearInterval(this.interval)
          this.second = 60
          this.showText = true
          this.interval = null
        }
      }, 1000)
    },
    //勾选免责
    Agree() {
      this.isAgree = !(this.isAgree)
    },
    //打开弹框
    gouserAgreementPage() {
      // this.showPopup = true

      uni.navigateTo({
        url: '/pages_logins/agCarpur/agCarpur'
      })
    },
    //同意免责
    agreepopup(type) { //type 0同意
      if (type === 0) {
        this.isAgree = false
        this.showPopup = false //关闭免责弹框
      } else {
        this.isAgree = true
        this.showPopup = false //关闭免责弹框
      }
    },
    //发送验证码
    async sendCode(type) {
      uni.showLoading({
        title: '发送中'
      })
      try {
        const res = await this.$api.order.SendVerficationCodes({
          mobile: this.mobile
        })
        if (res.code === 200) {
          uni.showToast({
            title: '短信发送成功',
            icon: 'none'
          })
          // this.verificationCode = res.data.statusCode//验证码
          uni.hideLoading()
          this.getCode() //倒计时
        }
      } catch (err) {
        // console.log(err)
        uni.hideLoading()
      }
    },
    //手机号输入
    InputChildItem: function(e) {
      if (e.detail.value.length === 11) {
        // this.yzsjh()//验证手机号
      }
    },
    // 输入手机号注册
    // async yzsjh() {
    //   try {
    //     const res = await this.$api.order.yzsjh({
    //       mobile: this.mobile //电话号码
    //     })
    //     if (res.data === 200) {
    //       this.registernot = true
    //     }
    //   } catch (err) {}
    // },
    //跳转品牌系列配置
    addbrands() {
      uni.navigateTo({
        url: '/pages_common/chooseSpecifications/chooseSpecifications?type=120&pageFrom=carpction'
      })
    },
    //立即询比价购车
    askBuyCar() {
      // if (this.mobile === '') {
      //   uni.showToast({
      //     title: '请输入手机号',
      //     duration: 3000,
      //     icon: 'none'
      //   })
      //   return false
      // }
      // if (this.grname === '') {
      //   uni.showToast({
      //     title: '请输入姓名',
      //     duration: 3000,
      //     icon: 'none'
      //   })
      //   return false
      // }
      // if (this.specifAll.brandlist.brand === '') {
      //   uni.showToast({
      //     title: '请选择车子',
      //     duration: 3000,
      //     icon: 'none'
      //   })
      //   return false
      // }

      if (this.isAgree === false) {
        this.showPopup = false
        this.AddCarCalculate()
      }else{
        this.showPopup = true
      }
    },
    //立即询比价购车接口
    async AddCarCalculate() {
      try {
        const res = await this.$api.order.AddCarCalculate({
          brand_Id: JSON.stringify(this.specifAll.brandlist.bid), //品牌Id
          brand_Name: this.specifAll.brandlist.brand, //品牌名称
          car_Type_Id: JSON.stringify(this.specifAll.Serieslist.carTypeId), //系列Id
          car_Type_Name: this.specifAll.Serieslist.carTypeName, //系列名称
          configuration_Id: this.specifAll.Configulist.id, //配置Id
          configuration_Name: this.specifAll.Configulist.name, //配置名称
          province: this.province, //省份
          city: this.city, //城市
          name: this.grname, //姓名
          mobile: this.mobile, //电话号码
          islogin: this.regtype, //是否登录
          verificationCode: this.verificationCode, //验证码
        })
        if (res.code === 200) {
          uni.navigateTo({
            url: '/pages_order/estimatedResults/estimatedResults?money=' + res.data.factoryprice
          })
        }

      } catch (err) {}
    },
  }
}
